package com.zpp.common.core.domain;

import com.zpp.common.core.enums.ResultCode;
import lombok.Data;

/**
 * 统一返回类型
 */

@Data
public class Result<T> {
    private int code;    //返回的状态码

    private String msg;  //返回的状态描述信息

    private T data;      //返回的数据对象

    /**
     * 封装通用返回方法
     */

    public static <T>Result<T> success () {
        return finalResult(null, ResultCode.SUCCESS);
    }

    public static <T>Result<T> success (T data) {
        return finalResult(data, ResultCode.SUCCESS);
    }

    public static <T>Result<T> fail () {
        return finalResult(null, ResultCode.FAILED);
    }

    public static <T>Result<T> fail (ResultCode resultCode) {
        return finalResult(null, resultCode);
    }

    public static <T>Result<T> fail (int code, String msg) {
        return finalResult(null, msg, code);
    }

    public static <T> Result<T> finalResult(T data, ResultCode resultCode) {
        Result<T> result = new Result<>();
        result.setData(data);
        result.setCode(resultCode.getCode());
        result.setMsg(resultCode.getMsg());
        return result;
    }

    public static <T> Result<T> finalResult(T data, String msg, int code) {
        Result<T> result = new Result<>();
        result.setData(data);
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

}
